.PHONY: help build build-cpp build-go clean rebuild docker docker-build docker-run test lint fmt

# Default target
.DEFAULT_GOAL := help

# Directories
CPP_DIR := YOLOv8-OpenVINO-CPP-Inference
GO_DIR := goyolo

help:
	@echo "YOLOv8 OpenVINO Go Service - Build Targets"
	@echo ""
	@echo "Build Targets:"
	@echo "  make build          - Build both C++ and Go components"
	@echo "  make build-cpp      - Build C++ component only"
	@echo "  make build-go       - Build Go component only"
	@echo "  make clean          - Clean all build artifacts"
	@echo "  make rebuild        - Clean and build"
	@echo ""
	@echo "Docker Targets:"
	@echo "  make docker-build   - Build Docker image"
	@echo "  make docker-run     - Run Docker container"
	@echo "  make docker-stop    - Stop Docker container"
	@echo ""
	@echo "Development Targets:"
	@echo "  make test           - Run tests"
	@echo "  make lint           - Run linters"
	@echo "  make fmt            - Format code"
	@echo ""
	@echo "Utility Targets:"
	@echo "  make help           - Show this help message"

build: build-cpp build-go
	@echo "✓ Build completed successfully"

build-cpp:
	@echo "Building C++ component..."
	@cd $(CPP_DIR) && make build
	@echo "✓ C++ build completed"

build-go:
	@echo "Building Go component..."
	@cd $(GO_DIR) && make build
	@echo "✓ Go build completed"

clean:
	@echo "Cleaning build artifacts..."
	@cd $(CPP_DIR) && make clean
	@cd $(GO_DIR) && make clean
	@echo "✓ Clean completed"

rebuild: clean build
	@echo "✓ Rebuild completed"

test:
	@echo "Running tests..."
	@cd $(GO_DIR) && go test -v ./...
	@echo "✓ Tests completed"

lint:
	@echo "Running linters..."
	@cd $(GO_DIR) && go vet ./...
	@echo "✓ Lint completed"

fmt:
	@echo "Formatting code..."
	@cd $(GO_DIR) && go fmt ./...
	@echo "✓ Format completed"

docker-build:
	@echo "Building Docker image..."
	@docker build -t yolov8-openvino-service:latest .
	@echo "✓ Docker image built"

docker-run:
	@echo "Starting Docker container..."
	@docker-compose up -d
	@echo "✓ Docker container started"
	@echo "Service available at http://localhost:8080"

docker-stop:
	@echo "Stopping Docker container..."
	@docker-compose down
	@echo "✓ Docker container stopped"

docker-logs:
	@docker-compose logs -f

# Development targets
dev-build-cpp:
	@cd $(CPP_DIR) && mkdir -p build && cd build && cmake .. && make

dev-build-go:
	@cd $(GO_DIR) && go build -o bin/goyolo-service-dev .

dev-run-go:
	@cd $(GO_DIR) && ./bin/goyolo-service-dev

# Installation targets
install: build
	@echo "Installing binaries..."
	@sudo install -m 755 $(CPP_DIR)/bin/detect_service /usr/local/bin/
	@sudo install -m 755 $(GO_DIR)/bin/goyolo-service /usr/local/bin/
	@echo "✓ Installation completed"

uninstall:
	@echo "Uninstalling binaries..."
	@sudo rm -f /usr/local/bin/detect_service
	@sudo rm -f /usr/local/bin/goyolo-service
	@echo "✓ Uninstallation completed"

# Info targets
info:
	@echo "Project Information:"
	@echo "  C++ Component: $(CPP_DIR)"
	@echo "  Go Component: $(GO_DIR)"
	@echo "  Docker Image: yolov8-openvino-service:latest"
	@echo ""
	@echo "Build Status:"
	@if [ -f "$(CPP_DIR)/bin/detect_service" ]; then echo "  ✓ C++ binary built"; else echo "  ✗ C++ binary not built"; fi
	@if [ -f "$(GO_DIR)/bin/goyolo-service" ]; then echo "  ✓ Go binary built"; else echo "  ✗ Go binary not built"; fi

# Quick start
quickstart: build
	@echo ""
	@echo "Quick Start:"
	@echo "1. Set environment variables:"
	@echo "   export MODEL_PATH=/path/to/yolov8n.xml"
	@echo "   export CPP_BINARY_PATH=$(PWD)/$(CPP_DIR)/bin/detect_service"
	@echo "   export PORT=8080"
	@echo ""
	@echo "2. Run the service:"
	@echo "   cd $(GO_DIR) && ./bin/goyolo-service"
	@echo ""
	@echo "3. Test the service:"
	@echo "   curl http://localhost:8080/health"

